define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('IIoT0101_Service',
        ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog',
            function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog) {
                this.init = function (options) {
                    var openDialog = function (data) {
                        $mdDialog.dialog(
                            'module/moduleIIoT/program/IIoT01/IIoT0101/IIoT0101.html',
                            function (dialog) {
                                return {
                                    lot_info_by_equipment_job: data.lot_info_by_equipment_job,
                                    calendar_day_range: data.calendar_day_range,
                                    equipment_no: options.equipment_no,
                                    css_url: 'module/moduleIIoT/program/IIoT01/IIoT0101/IIoT0101.css',
                                    init: function () {
                                        dialog.time_range = dialog.calendar_day_range.join(" ~ ");
                                        dialog.lot_info_by_equipment_job.forEach(function (item) {
                                            //預計派工時間湊顯示文字
                                            var disp_start_time = moment(item.disp_start_time).format('HH:mm');
                                            var disp_end_time = moment(item.disp_end_time).format('HH:mm');
                                            if (moment(item.disp_start_time).format('MM-DD') != moment().format('MM-DD'))
                                                disp_start_time = "*" + disp_start_time;
                                            if (moment(item.disp_end_time).format('MM-DD') != moment().format('MM-DD'))
                                                disp_end_time = "*" + disp_end_time;
                                            item.dispatch_time_range = disp_start_time + " ~ " + disp_end_time;
                                            //預估時間湊顯示文字
                                            if (item.formal_lot_state != "4") {
                                                var estimate_start_time = moment(item.estimate_start_time).format('HH:mm');
                                                var estimate_end_time = moment(item.estimate_end_time).format('HH:mm');
                                                if (moment(item.estimate_start_time).format('MM-DD') != moment().format('MM-DD'))
                                                    estimate_start_time = "*" + estimate_start_time;
                                                if (moment(item.estimate_end_time).format('MM-DD') != moment().format('MM-DD'))
                                                    estimate_end_time = "*" + estimate_end_time;
                                                item.estimate_time_range = estimate_start_time + " ~ " + estimate_end_time;
                                            }
                                            else {
                                                item.estimate_time_range = "";
                                            }
                                            item.lower_time_mark = false;
                                            item.lot_state_str = dialog.getStatusStr(item.formal_lot_state);
                                            item.all_qty_str = item.qty + "/" + item.out_qty + "/" + item.counter_qty;
                                            if (item.act_gap_minutes_str == "00:00")
                                                item.act_gap_minutes_str = "";
                                            else {
                                                var list_str = item.act_gap_minutes_str.split(":");
                                                item.act_gap_minutes_str = list_str[0] + " " + $filter('translate')('IIoT0101.hr') + " " + list_str[1] + " " + $filter('translate')('IIoT0101.minute');
                                            }
                                            if (item.act_gap_minutes > 0)
                                                item.lower_time_mark = true;
                                            return item;
                                        });
                                    },
                                    getStatusStr: function (lot_state) {
                                        //0:未到站、1:待進站、2:待出站、3:暫停中、4.已完工
                                        var str = "";
                                        switch (lot_state) {
                                            case "0":
                                                str = $filter('translate')('IIoT0101.status.status_0');
                                                break;
                                            case "1":
                                                str = $filter('translate')('IIoT0101.status.status_1');
                                                break;
                                            case "2":
                                                str = $filter('translate')('IIoT0101.status.status_2');
                                                break;
                                            case "3":
                                                str = $filter('translate')('IIoT0101.status.status_3');
                                                break;
                                            case "4":
                                                str = $filter('translate')('IIoT0101.status.status_4');
                                                break;
                                        }
                                        return str;
                                    },
                                    back: function () {
                                        options.feedback(dialog);
                                    }
                                }
                            }

                        );
                    }
                    var equipmentJobScheduleInfo = function () {
                        $MMWService.sendToServer({
                            uri: 'ServicesSTD.Module_IIoT.equipment_job_schedule_info_get',
                            content: {
                                equipment_no: options.equipment_no
                            },
                            success: function (data) {
                                if (data.lot_info_by_equipment_job
                                    && data.calendar_day_range
                                    && data.lot_info_by_equipment_job.length > 0
                                    && data.calendar_day_range.length > 0) {
                                    openDialog(data);
                                }

                            }
                        });
                    }
                    equipmentJobScheduleInfo();
                };
            }]);
});